<template>
  <transition name="slide">
    <div class="data-panel" v-if="point">
      <!-- 关闭按钮 -->
      <div class="close-btn" @click="$emit('close')">
        <el-icon><Close /></el-icon>
      </div>

      <!-- 图片展示区 -->
      <div class="image-section">
        <div v-if="point.images && point.images.length > 0" class="image-container">
          <img
              :src="currentImage.url"
              :alt="currentImage.title"
              class="main-image"
              @click="toggleImageFullscreen"
          />

          <!-- 图片导航 -->
          <div class="image-nav" v-if="point.images.length > 1">
            <el-button
                circle
                class="nav-btn prev"
                @click.stop="$emit('prev-image')"
            >
              <el-icon><ArrowLeft /></el-icon>
            </el-button>
            <el-button
                circle
                class="nav-btn next"
                @click.stop="$emit('next-image')"
            >
              <el-icon><ArrowRight /></el-icon>
            </el-button>
          </div>

          <!-- 图片计数 -->
          <div class="image-counter">
            {{ currentImageIndex + 1 }} / {{ point.images.length }}
          </div>
        </div>

        <div v-else class="no-image">
          <el-icon><Picture /></el-icon>
          <span>暂无相关图片</span>
        </div>
      </div>

      <!-- 内容区 -->
      <div class="content-section">
        <h2 class="point-title">{{ point.name }}</h2>
        <div class="coordinates">
          <span>坐标: {{ point.lat.toFixed(6) }}° N, {{ point.lng.toFixed(6) }}° E</span>
        </div>

        <!-- 评论区域 -->
        <div class="comments-section">
          <div class="comments-header">
            <h3>评论</h3>
            <span class="comment-count">{{ point.comments.length }} 条</span>
          </div>

          <!-- 评论列表 -->
          <div class="comments-list">
            <div
                v-for="comment in point.comments"
                :key="comment.id"
                class="comment-item"
            >
              <el-avatar :size="36" :src="comment.avatar" />
              <div class="comment-content">
                <div class="comment-header">
                  <span class="username">{{ comment.user }}</span>
                  <span class="time">{{ comment.time }}</span>
                </div>
                <div class="comment-text">{{ comment.content }}</div>
              </div>
            </div>

            <div v-if="point.comments.length === 0" class="no-comments">
              暂无评论，快来发表第一条评论吧！
            </div>
          </div>

          <!-- 发表评论 -->
          <div class="add-comment">
            <el-input
                v-model="newComment"
                placeholder="写下你的评论..."
                type="textarea"
                :rows="2"
                resize="none"
            />
            <el-button
                type="primary"
                class="submit-btn"
                @click="submitComment"
                :disabled="!newComment.trim()"
            >
              发布
            </el-button>
          </div>
        </div>
      </div>

      <!-- 全屏图片 -->
      <div v-if="fullscreenImage" class="image-fullscreen" @click="toggleImageFullscreen">
        <img :src="currentImage.url" :alt="currentImage.title" />
      </div>
    </div>
  </transition>
</template>

<script>
import { ref, computed } from 'vue'
import {
  Close,
  ArrowLeft,
  ArrowRight,
  Picture
} from '@element-plus/icons-vue'

export default {
  props: {
    point: {
      type: Object,
      default: null
    }
  },
  setup(props, { emit }) {
    const newComment = ref('')
    const fullscreenImage = ref(false)

    // 当前显示的图片索引
    const currentImageIndex = computed(() => {
      return props.point?.currentImageIndex || 0
    })

    // 当前显示的图片
    const currentImage = computed(() => {
      if (!props.point || !props.point.images || props.point.images.length === 0)
        return null

      return props.point.images[currentImageIndex.value]
    })

    // 提交评论
    const submitComment = () => {
      if (!newComment.value.trim()) return

      emit('add-comment', newComment.value)
      newComment.value = ''
    }

    // 切换全屏图片
    const toggleImageFullscreen = () => {
      if (!currentImage.value) return
      fullscreenImage.value = !fullscreenImage.value
      // 全屏时禁止页面滚动
      document.body.style.overflow = fullscreenImage.value ? 'hidden' : '';
    }

    return {
      newComment,
      currentImageIndex,
      currentImage,
      fullscreenImage,
      submitComment,
      toggleImageFullscreen
    }
  }
}
</script>

<style scoped>
.data-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80%;
  max-width: 900px;
  height: 85%;
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
  z-index: 2000;
  display: flex;
  overflow: hidden;
}

.slide-enter-active,
.slide-leave-active {
  transition: all 0.4s ease;
}

.slide-enter-from,
.slide-leave-to {
  opacity: 0;
  transform: translate(-50%, 100%);
}

.close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: all 0.3s;
}

.close-btn:hover {
  background: #f0f0f0;
  transform: scale(1.1);
}

.image-section {
  flex: 0 0 50%;
  background: #f8f8f8;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
}

.image-container {
  width: 100%;
  height: 100%;
  position: relative;
}

.main-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  cursor: zoom-in;
  transition: transform 0.3s;
}

.main-image:hover {
  transform: scale(1.02);
}

.image-nav {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  pointer-events: none;
}

.nav-btn {
  pointer-events: all;
  margin: 0 20px;
  background: rgba(255, 255, 255, 0.8);
  border: none;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.nav-btn:hover {
  background: white;
}

.prev {
  transform: translateX(-50%);
}

.next {
  transform: translateX(50%);
}

.image-counter {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 14px;
}

.no-image {
  display: flex;
  flex-direction: column;
  align-items: center;
  color: #999;
}

.no-image .el-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.content-section {
  flex: 1;
  padding: 30px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.point-title {
  margin: 0 0 10px 0;
  font-size: 24px;
  color: #333;
}

.coordinates {
  margin-bottom: 25px;
  color: #666;
  font-size: 14px;
}

.comments-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.comments-header {
  display: flex;
  align-items: center;
  margin-bottom: 16px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.comments-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

.comment-count {
  margin-left: 10px;
  color: #999;
  font-size: 14px;
}

.comments-list {
  flex: 1;
  overflow-y: auto;
  padding-right: 8px;
  margin-bottom: 16px;
}

.comment-item {
  display: flex;
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.comment-item:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.comment-content {
  flex: 1;
  margin-left: 12px;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}

.username {
  font-weight: 500;
  font-size: 15px;
}

.time {
  color: #999;
  font-size: 13px;
}

.comment-text {
  font-size: 14px;
  line-height: 1.5;
  color: #333;
}

.no-comments {
  padding: 30px 0;
  text-align: center;
  color: #999;
  font-size: 14px;
}

.add-comment {
  position: relative;
}

.submit-btn {
  position: absolute;
  bottom: 10px;
  right: 10px;
  font-size: 14px;
  padding: 6px 16px;
  border-radius: 16px;
}

.image-fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  max-width: 100%;
  max-height: 100%;
  background: rgb(255, 255, 255);
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  overflow: hidden; /* 禁止滚动，确保图片不超出 */
}

.image-fullscreen img {
  width: 100%;    /* 宽度不超过容器 */
  height: 100%;   /* 高度不超过容器 */
  object-fit: fill; /* 保持比例完整显示 */
  display: flex;
  background-color: #f0f0f0;
}

/* 适配大图：强制填满屏幕 */
@media (min-width: 768px) {
  .image-fullscreen img {
    max-width: none;
    max-height: none;
    width: auto;
    height: auto;
    min-width: 100%;
    min-height: 100%;
    object-fit: cover; /* 填满整个屏幕 */
  }
}
</style>
